//
//  CTMainTabBarController.m
//  CT
//
//  Created by liufanwei on 15/3/26.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTMainTabBarController.h"
#import "CommonMicro.h"

@interface CTMainTabBarController (){
    BOOL isDial;
    UITabBarItem * bohao;
}

@end

@implementation CTMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = self.tabBar.frame;
    CGRect frame = CGRectMake(0, 0, CGRectGetMaxX(rect), 49);
    UIView * v = [[UIView alloc] initWithFrame:frame];
    v.backgroundColor = kBackGroundColor;
    [self.tabBar insertSubview:v atIndex:0];
    self.tabBar.opaque = NO;
    [self.tabBar setTranslucent:NO];
    
    NSArray * items = [self.tabBar items];
    bohao = [items objectAtIndex:0];
    bohao.imageInsets = UIEdgeInsetsMake(10, 0, -10, 0);
    bohao.tag = 1;
    [self initItem:bohao normal:@"keyboardw" Select:@"keyboardy"];
    
    UITabBarItem * lianxiren = [items objectAtIndex:1];
    lianxiren.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
    [self initItem:lianxiren normal:@"contactw" Select:@"chontacty"];
    
    UITabBarItem * chongzhi  = [items objectAtIndex:2];
    chongzhi.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
    [self initItem:chongzhi normal:@"chargew" Select:@"chargey"];
    
    UITabBarItem * gengduo   = [items objectAtIndex:3];
    gengduo.imageInsets = UIEdgeInsetsMake(10, 0, -10, 0);
    [self initItem:gengduo normal:@"morew" Select:@"morey"];
    isDial = YES;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNotification:) name:kNotifiCationHidKeyboard object:Nil];
}


-(void)viewDidDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewDidDisappear:animated];
}

-(void)onNotification:(id)sender{
    [self initItem:bohao normal:@"keyboardw" Select:@"keyboardy"];
    isDial = NO;
}

-(void)initItem:(UITabBarItem*)item normal:(NSString*)name Select:(NSString*)selectName{
    item.selectedImage = [[UIImage imageNamed:selectName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item.image         = [[UIImage imageNamed:name] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    if (item.tag == 1) {
        if (isDial) {
            [self initItem:bohao normal:@"keyboardupb" Select:@"keyboardupy"];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiCationShowKeyboard object:[NSString stringWithFormat:@"%d",isDial]];
        }else{
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiCationHidKeyboard object:Nil];
        }
        isDial = !isDial;
    }
    else{
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiCationHidKeyboard object:Nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
